<?php
header('Content-Type: text/html; charset=iso-8859-1');
$wl = 250;
include ('../Connections/KRAPPERCON.php');
require('../Libs/fpdf/fpdf.php');
require('../Libs/fpdf/extend.php');
	$tipo = '';
	$dirLocal = '';
	$obsLocal = '';
	$placa = '';
	$vin = '';
	$FacturaID = '';
	$cliente = '';
	$vendedor = '';
	$fecha ='';
	$kardex = 0;
	$fact_neto = 0;
	$fact_igv = 0;
	$fact_tot = 0;
	$fecven = '';
	$ordenID = '';
	$cladoc = '';
	$condpag = '';
	$cldoc = '';
	$hora = '';
	$hora  = '';
	$direccion = '';
	$ruc = '';
	$fol = '';
	$moneda = '';
	$vehID = '';
	$showDct = 0;
	$usrid = '';
	

        
	$oper='';
	$factura='';	
		
if(isset($_GET['oper'])){
	$oper=$_GET['oper'];
}else{
    $oper='print';	
}

//$oper = $_GET['oper'];
//$oper = 'print';

if ($oper == 'print'){


 if( isset($_GET['factura']) ){
	 $factura=$_GET['factura'];
 }else{
	 $factura='9000009';
 }

//$factura = $_GET['factura'];
//$factura  = '200000074';

$query="call sp_printfactura ('".$factura."');";
//echo $query;

$res = $mysqli->query($query);
if ($res) {
	if ($row = $res->fetch_assoc() ){
	$FacturaID = $row['factura'];	
	$cliente = $row['nomcli'];
	$vendedor = $row['usrnom'];
	$condpag = $row['condvta'];
	$usrid = $row['user'];
	$fact_neto = $row['w_factNet'];
	$fact_igv = $row['w_factIva'];	
	$fact_tot = $fact_neto + $fact_igv;
	$ordenID = $row['ordenID'];
	$fecven = $row['fecven'];
	$moneda = $row['moneda'];
	$placa = $row['w_placa'];
	$vin = $row['w_vin'];
	$vehID = $row['w_veh'];
	
	if (trim($placa)==""){
		$placa = $vin;
	}
	if (trim($placa)==""){
		$placa = "Ser.".$vehID;
	}
    //$showDct = $row['showDct'];
	//$fecfac = $row['FacFecFac'];
	$cladoc = substr($row['cladoc'],3,2);
	$kardex  = $row['kardex'];
	if ($cladoc == 'BL'){
		$tipo = 'BOLETA';
	}
	elseif ($cladoc == 'FC'){
		$tipo = 'FACTURA';	
	}
	elseif ($cladoc == 'NF' || $cladoc == 'NB' ){
		$tipo = 'NOT.CREDITO';	
	}
	elseif ($cladoc == 'BA' || $cladoc == 'FA' ){
		$tipo = 'BOL.ANTICIPO';	
	}
	
	
	$fecha = $row['fecfac'];
	// 2012-12-12
	$newfec = substr($fecha,8,2)."/".substr($fecha,5,2)."/".substr($fecha,0,4);
	$hora = $row['hora'];
	//echo $hora;
	//$hora  = '';
	$texto = $row['texto'];
	$direccion = $row['dircli'];
	$ruc = $row['nifcli'];
	$folio = $row['folio'];	$dirLocal = $row['dirLocal'];
	$obsLocal = $row['obsLocal'];

	}
	$res->close();
	$mysqli->check_result();
}
$typ = 'D';
$sx = 5	;
$fnt = 'Arial';
//$pdf=new PDF('P','mm',array(225,222));
$pdf=new PDF('P','mm','A4');
$pdf->AddPage();
$pdf->AddFont('Dot','','dot.php');
$pdf->SetMargins(0,0,13);
$pdf->SetFont($fnt,'',18);
$pdf->SetAutoPageBreak(true, 0); 

$pdf->Ln(35);

//$pdf->Cell(0, 0, "LIMA", 0,0,'L');
//$pdf->Cell(0,5,'TOYOCARR S.A.C.',0,0,'L');
/*$pdf->SetFont($fnt,'B',10);
$pdf->Ln(7);
$pdf->SetX($sx);
$pdf->Cell(58, 5,'','T');
$pdf->SetX($sx);
$pdf->Cell(58, 5, "REPUESTOS ORIGINALES", 'LR',1,'L',false);
$pdf->SetX($sx);
$pdf->Cell(58, 5, "REPUESTOS ORIGINALES", 'LR',1,'L',false);
$pdf->SetX($sx);
$pdf->Cell(58, 5,'','T'); */

//Creamos una celda de 10 de ancho, 20 de alto, con ese texto en su interior, y 1 de borde.


//$pdf->Ln(2);







//$pdf->Cell($wl, 0, "", 1, 2, 'L', true);
$pdf->SetFont($fnt,'',10);
//$pdf->Ln(2);

$pdf->SetX($sx);
$pdf->Cell(115,7,str_repeat(' ', 24). utf8_decode($cliente), '', 1, 'L');

$pdf->SetX($sx);
$pdf->Cell(115,7,str_repeat(' ', 24). utf8_decode($direccion) , '', 0, 'L');
$pdf->SetX($sx + 110);
$pdf->Cell(115,7,$FacturaID,'',1,'C');
$pdf->SetX($sx);
$pdf->Cell(115,7,str_repeat(' ', 24).$ruc, '', 0, 'L');

$pdf->SetX($sx+86);
$pdf->Cell(0,7, str_repeat(' ',40). substr($newfec,0,2).str_repeat(' ',22).nameMes(substr($newfec,3,2)).str_repeat(' ',20).substr($newfec,6,4), '', 1, 'L');
$pdf->SetX($sx);
$pdf->SetFont($fnt,'',8);
$pdf->SetX($sx);
$pdf->Cell(115,3,'', '', 1, 'L');

$pdf->SetX($sx);
$pdf->SetFont($fnt,'',10);
$pdf->Cell(25, 7, $folio, '', 0,'C');
$pdf->Cell(35, 7, '','', 0, 'C');
$pdf->Cell(35, 7, $ordenID,'', 0, 'C');
$pdf->Cell(25, 7, strtoupper($placa),'', 0, 'C');
$pdf->Cell(35, 7, strtoupper($usrid),'', 0, 'C');
$pdf->Cell(42, 7, strtoupper(utf8_decode($condpag)),'', 0, 'C');
$pdf->SetX($sx);
$pdf->Ln(7);
$pdf->SetX($sx);
//$pdf->doLine();
$pdf->SetX($sx);
//$pdf->Cell(197, 7,'','T');

$pdf->Ln(4);
$pdf->SetX($sx);
//$pdf->doLine();
//$pdf->Cell(197,0,'','T',true);
$pdf->SetX($sx);
/*
                //detalle
			    $pdf->Cell(10,5, "ALM.", 0, 0, 'C');
				$pdf->Cell(95,5, "ARTICULO", 0, 0, 'C');
				$pdf->Cell(22,5, "CANTIDAD", 0, 0, 'R');
				$pdf->Cell(22,5, "DSCTO".($showDct==1?"(%)":""), 0, 0, 'R');
				//$pdf->Cell(25,5, "I.G.V", 0, 0, 'C');
				if ($kardex==1){
				$pdf->Cell(22,5, "PR.UNIT.S/.", 0, 0, 'R');
				}
				else {
				$pdf->Cell(22,5, "           	", 0, 0, 'R');
				}
				$pdf->Cell(22,5, "IMPORTE S/.", 0, 1, 'R');
				*/
		
$pdf->SetX($sx);
//pdf->Cell($wl, 0, "", 1, 2, 'L', true);
//$pdf->doLine();
//$pdf->Cell(197,0,'','T',true);
$pdf->SetX($sx);			
$pdf->Ln(2);			

$neto1 = 0;
$neto2 = 0;
$neto = 0;
$pru = 0;
$dscto = 0 ;
$query = "call sp_getFacturaDet('".$factura."')";
$res4 = $mysqli->query($query) or die($mysqli->error.__LINE__);
$igv = 0;
$igv_por = 18;

$print_dct = 0;

if ($res4) {
while ($row3 = $res4->fetch_assoc() )
			
			{
			    $pdf->SetX($sx);
				$pdf->SetFont('Arial','',10);
			    $pdf->Cell(10,5, $row3['DetFacQty'], 0, 0, 'R');
			    $pdf->Cell(30,5, $row3['DetFactUM'], 0, 0, 'C');
				$pdf->Cell(70,5,/*strtoupper($row3['ArticuloID']. " - ". */utf8_decode($row3['DetFactTxt']),0, 0, 'L');
                                /*
                                if ($showDct == 0){
                                $print_dct = fmt(abs($row3['DetFactDct']));
                                }
                                else {
                                $print_dct = fmt(100*$row3['DetFactDct']/$row3['DetFactPru'])  ;  
                                }
                                */
								
                                //$print_dct = $print_dct * 100;
                                
				//$pdf->Cell(22,5, $print_dct, 0, 0, 'R');
				//$pdf->Cell(25,5, abs($igv_por), 0, 0, 'C');
				if ($kardex == 1) {
				$pdf->Cell(50,5,fmt( abs($row3['DetFactPru'])), 0, 0, 'R');
				}
				else {
				$pdf->Cell(50,5,"", 0, 0, 'C');
				}
				
				$pdf->Cell(28,5, fmt(abs($row3['DetFactNet'])), 0, 1, 'R');
				$dscto = $dscto+abs($row3['DetFactDct']);
					
			}
$res4->close();
$mysqli->check_result();
			
			}

	$pdf->SetY(250);
    $pdf->SetX($sx);

	//$pn = $neto-$dscto;	
	//$igv =($pn) *($igv/100);
$tot = $igv + $neto2;
$pdf->SetFont($fnt,'',13);
$pdf->SetX($sx);
//$pdf->Cell(0,0,"Boleta de Anticipo",0,0,'L');
	
	$moneda_txt = getMonedaTxt($moneda);
	
	$query2="call sp_getNroletras ($fact_tot,'".''."');";

$res2 = $mysqli->query($query2) or die($mysqli->error.__LINE__);
if ($res2) {
	if($row2 = $res2->fetch_assoc()) {
	$txtmon = $row2['res'];
	if (trim($texto)!='') {
	$texto = 'NOTA: '.$texto;
	$pdf->Cell(125,5, $texto,0,1,'L');			
	$pdf->Cell(125,5,'',0,1,'L');
	}
	$pdf->SetX($sx);
    $pdf->Cell(200,5,str_repeat(' ',10).$txtmon.' '.$moneda_txt, 0, 1, 'L');
	}
	$res2->close();
$mysqli->check_result();
}


$pdf->SetX($sx);	
$pdf->SetFont($fnt,'',12);

$pdf->SetX($sx);
/*
$pdf->Cell(25, 5, 'CONTAB', 'LR', 0,'C');
$pdf->Cell(15, 5, 'V.B.','LR', 0, 'C');
$pdf->Cell(35, 5, 'CANCELADO','LR', 0, 'C');
$pdf->Cell(25, 5, 'VALOR BRUTO','LR', 0, 'C');
$pdf->Cell(15, 5, 'DCTO','LR', 0, 'C');
$pdf->Cell(32, 5, 'VALOR DE VENTA','LR', 0, 'C');
$pdf->Cell(25, 5, 'I.G.V.','LR', 0, 'C');
$pdf->Cell(25, 5, 'TOTAL','LR', 0, 'C');
*/
//$pdf->Cell(25,5,'','',0,'');

$pdf->SetX($sx);
//$pdf->doLine();
$pdf->SetX($sx);
//$pdf->Cell(197, 5,'','T');
$pdf->SetX($sx);
//$pdf->Cell(197, 5,'','T');
$pdf->Ln(6);
$pdf->SetX($sx);

$pdf->Cell(97, 5, '','', 0, 'R');
$pdf->Cell(12, 5, '','', 0, 'R');
$pdf->Cell(22, 5, myRound($fact_neto) ,'', 0, 'R');
$pdf->Cell(22, 5, myRound($fact_igv) ,'', 0, 'R');
$pdf->Cell(25, 5, myRound($fact_tot),'', 0, 'R');
$pdf->SetX($sx);
$pdf->Ln(5);
$pdf->SetX($sx);
//$pdf->Cell(197, 7,'','T');
//$pdf->Cell(195, 12,'ASDASDD','T');

/*if ($kardex == 1){
$pdf->Cell(18,5, "Subt. S/.", 0, 0, 'R');
$pdf->Cell(24,5, number_format($neto1,2), 0, 0, 'L');
}
else {
$pdf->Cell(18,5, "         ", 0, 0, 'R');
$pdf->Cell(24,5, "", 0, 0, 'L');
}
$pdf->Cell(20,5, "Dscto S/.", 0, 0, 'R');
$pdf->Cell(20,5, number_format($dscto,2), 0, 0, 'L');
$pdf->Cell(20,5, "Neto S/.", 0, 0, 'R');
$pdf->Cell(20,5,number_format($neto2,2)  , 0, 0, 'L');
$pdf->Cell(20,5, "IGV ".$igv_por." S/.", 0, 0, 'R');
$pdf->Cell(20,5, number_format($igv,2) , 0, 0, 'L');
$pdf->Cell(20,5, "Total S/.", 0, 0, 'R');
$pdf->Cell(35,5,number_format($tot,2) , 0, 1, 'L');*/
$pdf->SetX($sx);
//	$pdf->Cell($wl, 0, "", 1, 1, 'L', true);
//$pdf->doLine();
//$pdf->SetX(230);
//$pdf->Cell(16,5, "P.V.P S/.:", 0, 0, 'R');
//$pdf->Cell(30,5,number_format( $tot,2), 0, 1, 'L');

//$pdf->Output('Factura_'.$FacturaID.'.pdf',$typ);
//$pdf->Output('Factura_'.$FacturaID.'.pdf','I');
$pdf->Output('Factura_'.$FacturaID.'.pdf','D');

}


?>